<template>
  <section class="page-container">
    <DynamicForm :config="dyConfig" :questions="dyQuestions" />
    <DynamicTable :tables="dyTable" :config="dyTableConfig" :icons="dyIcons" />
  </section>
</template>

<script lang="ts" setup>
import { DynamicForm, DynamicTable } from '@/components/public-template';
import { Api } from '@/config/api/archivesBorrow';
import { filter } from 'rxjs/operators';
import { getCurrentInstance, onBeforeUnmount, reactive } from 'vue';
import { useRouter } from 'vue-router';
import indexService from './index.service';

const router = useRouter();
const { proxy }: any = getCurrentInstance();

const dyQuestions = reactive(indexService.getQuestions());
const dyConfig: any = {
  labelCol: 6,
  wrapperCol: 18,
  grid: 8,
  style: 'table-form',
  cancelSave: true
};

const dyTable = reactive(indexService.getTables());
const dyIcons = reactive(indexService.getIcons());
const dyTableConfig: any = {
  queryUrl: Api.archivesBorrowPage,
  protType: 'post',
  isScreen: true
};

const subscribeSub = proxy.$NotificationPubSub
  .getParams()
  .pipe(filter((res) => res !== 0))
  .subscribe((item) => {
    if (item.key === 'DYNAMICLOCALICONS' && item.value.value) {
      router.push({ name: 'ArchivesManageAddBorrow', query: { showType: 'ADD' } });
    }
  });

onBeforeUnmount(() => {
  subscribeSub && subscribeSub.unsubscribe();
  proxy.$NotificationPubSub.clearParams();
});
</script>
